﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xplang.run;
using xplang.abstractions;
using System.Diagnostics;

namespace xplang.statements
{
  [DebuggerDisplay("return {expression}")]
  public class Return : Statement
  {

    public Return(Expression expression)
    {
      this.expression = expression;
    }

    public readonly Expression expression;

    public override Value doIt(Value[] heap, ref FlowState state)
    {
      if (expression != null)
        heap[heap.Length - 1] = expression.doIt(heap, ref state);
      if (state == FlowState.Normal)
        state = FlowState.ExitFromMethod;
      return null;
    }

    public override bool isBlock()
    {
      return false;
    }

    //public override void save(Scope scope, SourceBuilder target)
    //{
    //    target.write("return");
    //    if (expression != null)
    //    {
    //        target.write(" ");
    //        expression.save(scope,target);
    //    }
    //    target.writeln(";");
    //}

    //public override void toCSharp(Scope scope, SourceBuilder target)
    //{
    //    target.write("return");
    //    if (expression != null)
    //    {
    //        target.write(" ");
    //        expression.toCSharp(scope, target);
    //    }
    //    target.writeln(";");
    //}

    //public override void toJavaScript(Scope scope, SourceBuilder target)
    //{
    //    target.write("return");
    //    if (expression != null)
    //    {
    //        target.write(" ");
    //        expression.toCSharp(scope, target);
    //    }
    //    target.writeln(";");
    //}
  }
}
